<template>
  <!-- 入催统计 -->
  <section class="page case-info-query-report">
    <data-form
      :submitLoading="submitLoading"
      :model="model"
      @onSearch="refreshData"
      @handleResetForm="handleResetForm"
      :page="pageService"
    >
      <template slot="inputs">
        <el-form-item label="借据号" prop="caseNumber">
          <el-input v-model="model.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="model.personalName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="model.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="model.idCard"></el-input>
        </el-form-item>
        <el-form-item label="案件金额" prop="overdueAmount">
          <number-range v-model="model.overdueAmount"></number-range>
        </el-form-item>
        <el-form-item label="逾期阶段" prop="overduePeriod">
          <el-select v-model="model.overduePeriod" placeholder="请选择" clearable>
            <el-option
              v-for="{index,name,code} in $dict.getDictData('OverduePeriodStatus')"
              :key="index"
              :label="name"
              :value="code.replace(/M|\+/, '')"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逾期天数" prop="overdueDays">
          <number-range v-model="model.overdueDays" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="系统来源" prop="sysCode">
          <el-select v-model="model.sysCode" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('SysCode')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="model.productName"></el-input>
        </el-form-item>
        <el-form-item label="地区" prop="area">
          <el-cascader
            v-model="model.area"
            :options="$city.getCityData()"
            change-on-select
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="机构" prop="deptIds">
          <el-cascader
            v-model="model.deptIds"
            :options="departmentData"
            @change="getcupoName"
            :props="{value:'id',label:'deptName'}"
            change-on-select
            :show-all-levels="false"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="催收员" prop="currentCollectorRealName">
          <el-input v-model="model.currentCollectorRealName"></el-input>
        </el-form-item>
        <el-form-item label="案件状态" prop="collectionStatus">
          <el-select v-model="model.collectionStatus" placeholder="请选择" clearable>
            <el-option
              v-for="{index,name,code} in collectionStatus"
              :key="index"
              :label="name"
              :value="code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户标签" prop="personalLabel">
          <el-select
            style="text-align: center;"
            v-model="model.personalLabel"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="{index,name} in customDataDict.filter(v=>v.dataType==='PERSONAL_LABEL').filter(m=>m.name!=='客户标签')"
              :key="index"
              :label="name"
              :value="name"
            ></el-option>
          </el-select>
        </el-form-item>
      </template>
    </data-form>
    <data-box
      :data="dataSet"
      :sort="sortService"
      :selectionList.sync="selectionList"
      :page="pageService"
      @onPageChange="refreshData"
    >
      <template slot="buttons">
        <el-button @click="exportCaseInfo" v-auth="2009">案件导出</el-button>
        <!-- <el-button @click="exportFollowup" v-auth="2010">催记导出</el-button> -->
      </template>
      <template slot="columns">
        <!-- <el-table-column label="操作" :min-width="$helper.getOperateWidth(0)">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="caseId = scope.row.caseId, dialog.addCaseRemark = true"
            >批注</el-button>
          </template>
        </el-table-column> -->
        <el-table-column prop="caseNumber" label="借据号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <a  @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</a>
          </template>
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip></el-table-column>
        <el-table-column
          prop="mobileNo"
          label="手机号"
          :formatter="(row)=>$filter.encryptPhone(row.mobileNo)"
          :min-width="$helper.getColumnWidth(4)"
        ></el-table-column>
        <el-table-column
          prop="idCard"
          label="身份证号"
          :formatter="(row)=>$filter.encryptIDCardFour(row.idCard)"
          :min-width="$helper.getColumnWidth(6)"
        ></el-table-column>
        <el-table-column
          prop="areaCode"
          label="地区"
          :min-width="$helper.getColumnWidth(4)"
          :formatter="(row) => $city.getInheritNames(Number(row.areaCode))"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="followInTime"
          label="案件流入时间"
          :formatter="(row) => $filter.dateFormat(row.followInTime)"
          :min-width="$helper.getColumnWidth(4)"
        ></el-table-column>
        <el-table-column
          prop="overduePeriod"
          label="逾期阶段"
          :min-width="$helper.getColumnWidth(3)"
        ></el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)"></el-table-column>
        <el-table-column prop="sysCode" label="系统来源" :formatter="(row) => $filter.encryptSysCode(row.sysCode)" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="productName" label="产品名称" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column
          prop="departmentType"
          label="案件类型"
          :min-width="$helper.getColumnWidth(4)"
        ></el-table-column>
        <el-table-column
          prop="collectionStatus"
          label="案件状态"
          :formatter="(row) => $filter.dictConvert(row.collectionStatus,'CollectionStatus')"
          :min-width="$helper.getColumnWidth(4)"
        ></el-table-column>
        <el-table-column prop="loanAmount" label="贷款金额(元)" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column
          prop="overdueAmount"
          label="案件金额(元)"
          :min-width="$helper.getColumnWidth(4)"
        ></el-table-column>
        <el-table-column prop="deptName" label="机构" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="collectorName" label="催收员" :min-width="$helper.getColumnWidth(4)"></el-table-column>
        <el-table-column prop="personalLabel" label="客户标签" :min-width="$helper.getColumnWidth(4)"></el-table-column>
      </template>
    </data-box>
    <el-dialog
      title="添加批注"
      :visible.sync="dialog.addCaseRemark"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      append-to-body
    >
      <add-case-remark
        @close="dialog.addCaseRemark=false;refreshData()"
        :caseId="caseId"
        ref="add-case-remark"
      ></add-case-remark>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Auth, Layout, Dependencies } from "~/core/decorator";
import { PageService } from "~/utils/page.service";
import { SortService } from "~/utils/sort.service";
import DataForm from "~/components/common/data-form.vue";
import DataBox from "~/components/common/data-box.vue";
import NumberRange from "~/components/common/number-range.vue";
import {State, Getter, Mutation, namespace } from "vuex-class";
import AddCaseRemark from "~/components/collection-execution/operate-components/add-case-remark.vue";
import {
  ApplyType,
  CASE_CHANNEL,
  CASE_HALT_CATEGORY
} from "~/config/enum.config";
import { OpenExecutionData } from "~/config/types";
import { SynthesizeQueryService } from "~/services/domain-service/synthesize-query.service";

const CollectionExecutionModule = namespace("collection-execution");

@Layout("workspace")
@Component({
  components: {
    DataForm,
    DataBox,
    NumberRange,
    AddCaseRemark
  }
})
export default class CaseInfoQueryReport extends Vue {
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(SortService)
  private sortService: SortService;
  @Dependencies(SynthesizeQueryService)
  private synthesizeQuerService: SynthesizeQueryService;
  @CollectionExecutionModule.Action private openCollectionExecution;
  @Getter
  private departmentData;
   @State private customDataDict;

  private dataSet: any[] = [];
  private selectionList: any[] = [];
  private dialog: any = {
    addCaseRemark: false
  };

  private caseId: string = "";
  private submitLoading: any = true

  private model: any = {
    caseNumber: "",
    personalName: "",
    mobileNo: "",
    idCard: "",
    currentCollectorRealName: "",
    collectionStatus: "",
    overdueAmount: {
      max: "",
      min: ""
    },
    overduePeriod: "",
    overdueDays: {
      max: "",
      min: ""
    },
    area: [],
    personalLabel:'',
    sysCode:''
  };

  private mounted() {
    this.refreshData();
  }

  private refreshData() {
    this.selectionList = []
    this.synthesizeQuerService
      .caseSearch(this.model, this.pageService)
      .subscribe(data => {
        this.dataSet = data
        if(this.dataSet){
          this.dataSet.forEach(x=>{
            x.overduePeriod>12?x.overduePeriod = 'M12+':x.overduePeriod = 'M'+x.overduePeriod
          })
        }
        this.submitLoading = false
      });
  }

  /**
   * 表单范围输入框重置
   */
  private handleResetForm() {
    this.model.overdueAmount = { min: "", max: "" };
    this.model.overdueDays = { min: "", max: "" };
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.openCollectionExecution(
      new OpenExecutionData(
        row.caseId,
        row.caseNumber,
        row.departmentType,
        true,
        true
      )
    );
  }
  /**
   * 导出案件
   */
  private exportCaseInfo() {
    if(this.dataSet.length === 0){
      this.$message("暂无数据可以导出")
      return
    }
    const ids =this.selectionList? this.selectionList.map((v) => v.caseId):'';
    const request: any = {
      ...this.model,
      caseIds: ids
    };
    this.synthesizeQuerService
      .caseSearchExportCase(request)
      .subscribe(data => this.$common.downloadFile(data.url));
  }

  /**
   * 导出催记
   */
  private exportFollowup() {
    const ids = this.selectionList.map(v => v.caseId);
    const request: any = {
      ...this.model,
      caseIds: ids
    };
    this.synthesizeQuerService
      .caseSearchExportFollow(request)
      .subscribe(data => this.$common.downloadFile(data.url));
  }

  /**
   * 获取机构id
   */
  private getcupoName() {
    this.model.deptId =
      this.model.deptIds.length > 0
        ? this.model.deptIds[this.model.deptIds.length - 1]
        : "";
  }

  //过滤案件状态类型
  private get collectionStatus() {
    const enableCollectionStatus: any = [
      "INNER_COLLECTING",
      "INNER_BANKC",
      "INNER_PAYOFF"
    ];
    return this.$dict
      .getDictData("CollectionStatus")
      .filter(v => enableCollectionStatus.includes(v.code));
  }
}
</script>

<style lang="less" scoped>
</style>
